﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using ihongma.Common;
using System.Drawing;
using System.IO;
using Ivony.Html;
using System.Data;
using Ivony.Html.Parser;

namespace ihongma.test
{
    public partial class WebForm4 : System.Web.UI.Page
    {
        JumonyParser parser = new JumonyParser();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Browser.Browser == "IE")//判断是否支持脚本
            {
                string temp = Request.UserAgent;
                int AV = int.Parse(temp.Substring(temp.IndexOf('/') + 1, 1));
                if (AV >= 5)
                {
                    Response.Write(true);
                }
                else
                {
                    Response.Write(false);
                }
            }
            else
            {
                Response.Write(true);
            }
            //string str = "http://uisdc.qiniudn.com/wp-content/uploads/2013/07/Softmatic-ScreenLayers-.jpg";
            //string temp =str.Substring(str.LastIndexOf("."));
            //Response.Write(temp);
            // BLL.article article = new BLL.article();
            //List<admodel> ad = new List<admodel>();
            //ad.Sort(new admodel());
            //ad.Reverse();
            //var tab = article.GetList(2, "", "id desc").Tables[0];

            //foreach (DataRow item in tab.Rows)
            //{
            //    var dataDocument = parser.Parse(item["content"].ToString());
            //      var infolist=sort(dataDocument.Find("img"));
            //      infolist.Reverse();
            //      if (infolist.Count > 0)
            //      {
            //          foreach (var i in infolist)
            //          {
            //              Response.Write(i.CountColor+"<br/>");
            //              Response.Write("<img width='270' height='129' src='"+i.ImgUrl+"'/><br/>");
            //          }
            //      }
            //}
        }
        private List<imgmodel> sort(IEnumerable<IHtmlElement> imgList)
        {
            string str = string.Empty;
            Stream stream = null;
            Bitmap original = null;
            List<imgmodel> modellist = new List<imgmodel>();

            foreach (var i in imgList)
            {

                try
                {


                    stream = Thumbnail.GetRemoteImage(i.Attribute("src").Value());
                    if (stream != null)
                    {
                        original = new Bitmap(stream);
                        if (original.Width >= 470 && original.Height >= 260)
                        {
                            imgmodel model = new imgmodel();

                            model.CountColor = CountImageColors(original);
                            model.ImgUrl = i.Attribute("src").Value();
                            modellist.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {

                    //logger.Error(ex.ToString());
                }
                //finally
                //{
                //    stream.Dispose();
                //    original.Dispose();
                //}

            }
            modellist.Sort(new imgmodel());

            return modellist;

        }
        public int CountImageColors(Bitmap bmp)
        {
            int count = 0;
            HashSet<Color> colors = new HashSet<Color>();



            try
            {

                if (bmp != null)
                {
                    for (int y = 0; y < bmp.Size.Height; y++)
                    {
                        for (int x = 0; x < bmp.Size.Width; x++)
                        {
                            colors.Add(bmp.GetPixel(x, y));
                        }
                    }

                }
                count = colors.Count;
            }
            catch
            {
                throw;
            }
            finally
            {
                colors.Clear();

            }

            return count;
        }
    }

    public class imgmodel : IComparer<imgmodel>
    {
        public int CountColor { get; set; }
        public string ImgUrl { get; set; }




        #region IComparer 成员

        public int Compare(imgmodel x, imgmodel y)
        {
            return x.CountColor.CompareTo(y.CountColor);
        }

        #endregion
    }
    public class admodel : IComparer<admodel>
    {
        public ihongma.Web.Plugin.Advert.Model.advert_banner advert_banner { get; set; }
        public int CountColor { get; set; }
        public string ImgUrl { get; set; }
        public int Compare(admodel x, admodel y)
        {
            return x.CountColor.CompareTo(y.CountColor);
        }
    }

}